﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.IO;
using TravelAgencyDAL;
using System.Drawing;

namespace TravelAgencyHTML5Client
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class ServiceTravelAgency : ITravelAgencyService
    {
        private const int PAGE_SIZE = 12;
        private TravelAgencyDAO dao = new TravelAgencyDAO();

        public IEnumerable<TripDTO> GetTrips(string page)
        {
            int requestedPage = int.Parse(page);
            var trips = dao.GetTripsByPage(PAGE_SIZE, requestedPage);
            var tripDtos = ConvertToTripDTO(trips);
            return tripDtos;
        }

        public string GetImage(string imageId)
        {
            if (imageId != null)
            {
                int selectedImage = int.Parse(imageId);
                Photo photo = dao.GetPhotoByPhotoId(selectedImage);
                return Convert.ToBase64String(photo.PhotoContent);

            }
            else
            {
                return @"..\Images\noImage.jpg";
            }
        }

        public TripDetailsDTO GetTripDetails(string id)
        {
            int tripId = int.Parse(id);
            Trip trip = dao.GetTripByTripId(tripId);
            TripDetailsDTO tripDtos = ConvertToTripDetailsDTO(trip);
            return tripDtos;
        }

        public void CreateReservation(WebReservation newReservation)
        {
            dao.AddReservation(newReservation.UserName, newReservation.TripId, newReservation.PeopleCount);
        }

        public static IEnumerable<TripDTO> ConvertToTripDTO(IEnumerable<Trip> trips)
        {
            List<TripDTO> converted = new List<TripDTO>();
            foreach (var trip in trips)
            {
                TripDTO toAdd = new TripDTO
                {
                    Id = trip.TripId,
                    Title = trip.Title,
                    StartDate = trip.StartDate,
                    EndDate = trip.EndDate,
                };
                if (trip.PhotoId != null)
                {
                    toAdd.PhotoId = trip.PhotoId;
                    toAdd.Image = Convert.ToBase64String(trip.Photo.PhotoContent);
                }
                else 
                {
                    //toAdd.Image = getDefaultImage();
                }
                converted.Add(toAdd);
            }
            return converted;
        }

        private static string getDefaultImage() 
        {
            string encodedImage;
            using (FileStream stream = new FileStream(@"noImage.jpg", FileMode.Open))
            {
                byte[] buf = new byte[4096];
                StringBuilder convertedImage = new StringBuilder();
                while (true)
                {
                    int bytesRead = stream.Read(buf, 0, buf.Length);
                    if (bytesRead == 0)
                    {
                        break;
                    }
                    string convertedBytes = Convert.ToBase64String(buf, 0, bytesRead);
                    convertedImage.Append(convertedBytes);
                }
                encodedImage = convertedImage.ToString();
            }
            return encodedImage;
        }

        public static TripDetailsDTO ConvertToTripDetailsDTO(Trip trip)
        {
            TripDetailsDTO converted = new TripDetailsDTO
            {
                AvailableRooms = trip.MaxPeopleCapacity - trip.CurrentPeopleCapacity,
                Discription = trip.Description,
                Price = trip.Price,
                Title = trip.Title,
                StartDate = trip.StartDate,
                EndDate = trip.EndDate,
                Image = Convert.ToBase64String(trip.Photo.PhotoContent)
            };
            if (trip.PhotoId != null)
            {
                converted.Image = Convert.ToBase64String(trip.Photo.PhotoContent);
            }
            else
            {
                //converted.Image = getDefaultImage();
            }
            return converted;
        }
    }
}
